<template>
  <a-card :loading="false" :bordered="false">
    <!-- 业务表单 -->
    <a-form-model ref="form" :model="formData" :rules="formRules" :form="form" style="margin-top: 8px">
      <!-- 组织名称 -->
      <a-form-model-item label="组织名称" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" prop="name">
        <a-input placeholder="请输入" v-model="formData.name" :disabled="!editable || editableFiexd['name']" />
      </a-form-model-item>

      <!-- 所属部门 -->
      <a-form-model-item label="所属部门" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" prop="sysOrgCode">
        <j-select-depart
          v-model="formData.sysOrgCode"
          :disabled="!editable || editableFiexd['sysOrgCode']"
          @selected="setDepart"
        />
      </a-form-model-item>

      <!-- 所属负责人 -->
      <a-form-model-item
        model-item
        label="组织负责人"
        :labelCol="{ span: 5 }"
        :wrapperCol="{ span: 15 }"
        prop="directorId"
      >
        <x-select-user-by-dep
          v-model="formData.directorId"
          :multi="false"
          :disabled="isFlow || !editable || editableFiexd['directorId']"
          @selected="setDirector"
        ></x-select-user-by-dep>
      </a-form-model-item>

      <a-form-model-item prop="pics" label="图片" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-image-upload
          text="上传图片"
          v-model="formData.pics"
          bizPath="std_group"
          :disabled="!editable"
          :isMultiple="true"
        ></j-image-upload>
      </a-form-model-item>

      <!-- 上传附件 -->
      <a-form-model-item prop="enclosures" label="附件" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-upload :disabled="!editable" v-model="formData.enclosures" text="上传文件" bizPath="std_group"></j-upload>
      </a-form-model-item>

      <!-- 指导老师 -->
      <a-form-model-item prop="instructorId" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="指导老师">
        <x-select-user-by-dep
          v-model="formData.instructorId"
          :disabled="!editable || editableFiexd['instructorId']"
          :multi="false"
          @selected="setInstructor"
        ></x-select-user-by-dep>
      </a-form-model-item>

      <!-- 成立时间 -->
      <a-form-model-item
        prop="buildTime"
        v-if="showBuildTime"
        label="成立时间"
        :labelCol="{ span: 5 }"
        :wrapperCol="{ span: 15 }"
      >
        <j-date
          placeholder="请选择日期"
          :disabled="!editable"
          v-model="formData.buildTime"
          :trigger-change="true"
          style="width: 100%"
        />
      </a-form-model-item>

      <!-- 解散时间 -->
      <a-form-model-item
        v-if="canDismiss"
        label="解散时间"
        :disabled="!editable"
        :labelCol="{ span: 5 }"
        :wrapperCol="{ span: 15 }"
      >
        <j-date placeholder="请选择解散时间！" v-model="formData.dismissTime" style="width: 100%" />
      </a-form-model-item>
      <slot></slot>
    </a-form-model>
  </a-card>
</template>

<script>
import JDate from '@/components/jeecg/JDate.vue'
import XSearchUser from '@/components/extends/XSearchUser'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JUpload from '@/components/jeecg/JUpload'
import { httpAction, getAction } from '@/api/manage'
import { getOrganizationById } from '@/api/api'
import pick from 'lodash.pick'
export default {
  name: 'Organization',
  components: {
    JSelectDepart,
    JSelectMultiUser,
    XSelectUserByDep,
    XSearchUser,
    JImageUpload,
    JUpload,
    JDate,
  },
  model: {
    prop: 'data',
    event: 'onchange',
  },
  props: {
    isFlow: Boolean,
    dataId: String,
    data: {
      type: Object,
      default: () => {
        return {}
      },
    },
    showBuildTime: Boolean,
    canDismiss: {
      type: Boolean,
      default: false,
    },
    editable: {
      type: Boolean,
      default: true,
    },
    editableFiexd: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      userDisabled: false,
      imageList: [],
      fileList: [],
      BaseUrl: window._CONFIG['domianURL'],
      loading: false,
      url: {
        add: '/org/stdGroup/add',
        edit: '/org/stdGroup/edit',
        queryById: '/org/stdGroup/queryById',
      },
      form: this.$form.createForm(this),
      formData: {
        creatorId: '',
        creatorName: '',
        director: '',
        directorId: '',
        id: '',
        instructor: '',
        name: '',
        sysOrgCode: '',
        departName: '',
        enclosures: '',
      },
      formRules: {
        name: [{ required: true, message: '请输入组织名称！' }],
        sysOrgCode: [{ required: true, message: '请选择所属部门！' }],
        directorId: [{ required: true, message: '请选择所属部门！' }],
        instructorId: [{ required: true, message: '请选择指导老师！' }],
        buildTime: [{ required: true, message: '请选择成立时间！' }],
      },
    }
  },
  beforeMount() {
    if (this.dataId) {
      this.queryData(this.dataId)
    } else {
      this.loading = false
    }
  },
  created() {
    this.fileList = ''
  },
  methods: {
    add(data) {
      this.userDisabled = true
      this.open(data)
    },
    apply(data) {
      this.userDisabled = true
      this.open(
        Object.assign({}, data, {
          workflowModel: {
            operation: 'start',
          },
        })
      )
    },
    edit(record) {
      this.userDisabled = true
      this.open(record)
    },
    open(record) {
      this.form.resetFields()
      this.loading = false
      if (record.id) {
        this.queryData(record.id).then(() => {
          if (record.workflowModel) Object.assign(this.formData, { workflowModel: record.workflowModel })
        })
      } else {
        this.formData = Object.assign({}, record)
      }
    },
    setDepart(data) {
      this.formData.departName = data.name
    },
    setDirector(data) {
      this.formData.director = data.name
    },
    setInstructor(data) {
      this.formData.instructor = data.name
    },
    queryData(id) {
      this.loading = true
      let params = { id }
      return getAction(this.url.queryById, params)
        .then((res) => {
          if (res.success) {
            this.formData = Object.assign({}, this.formData, res.result)
          }
          return res
        })
        .finally(() => {
          this.loading = false
        })
    },
    submitForm() {
      const that = this
      return new Promise((resolve, reject) => {
        // 触发表单验证
        that.$refs.form.validate((valid, fields) => {
          if (valid) {
            let httpurl = ''
            let method = ''
            if (!that.formData.id) {
              httpurl += that.url.add
              method = 'post'
            } else {
              httpurl += that.url.add
              method = 'post'
            }
            let formData = Object.assign({}, that.formData)
            httpAction(httpurl, formData, method)
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                  resolve(res)
                } else {
                  that.$message.warning(res.message)
                  reject(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          } else {
            that.$message.warning('请检查表单信息')
          }
        })
      })
    },
  },
  watch: {
    dataId: {
      handler(nv, ov) {
        if (nv && nv != ov) {
          this.formData = {}
          this.queryData(nv)
        }
      },
      immediate: true,
    },
    data: {
      handler(nv, ov) {
        // this.formData = {}
        // Object.assign(this.formData, nv)
        console.log(nv)
        this.formData = nv
      },
      immediate: true,
      deep: true,
    },
    formData: {
      handler(nv, ov) {
        if (nv != ov) this.$emit('onchange', nv)
      },
      immediate: true,
      deep: true,
    },
    fileList: {
      handler(nv, ov) {
        this.formData.enclosures = this.fileList
      },
    },
  },
}
</script>

<style></style>
